<?php

namespace common\service\task_list_sorter;

use common\model\SaasAttributeValueModel;
use common\service\TaskListSorterInterface;

class AssetTaskListSorter implements TaskListSorterInterface
{
    protected array $gradeScore = [];

    /**
     * 资产排序使用等级属性系数进行排序
     * @param $taskList
     * @return void
     */
    public function sortTaskList(&$taskList)
    {
        usort($taskList, function ($a, $b) {
            $av = $this->getGradeScore()[$a['grade'] ?? ''] ?? 0;
            $bv = $this->getGradeScore()[$b['grade'] ?? ''] ?? 0;
            if ($av == $bv) {
                return 0;
            } else if ($av > $bv) {
                return 1;
            } else {
                return -1;
            }
        });
    }

    /**
     * @return array
     */
    public function getGradeScore(): array
    {
        if (empty($this->gradeScore)) {
            $this->gradeScore = model(SaasAttributeValueModel::class)->select();
            $this->gradeScore = array_column($this->gradeScore, 'variable', 'value');
        }
        return $this->gradeScore;
    }
}
